<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="900px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="128px"
      :model="formData"
      :rules="rules"
      size="medium"
    >
      <el-form-item v-if="machineVisible" label="机器" prop="machine">
        <el-select
          v-model="formData.machine"
          clearable
          filterable
          placeholder="机器"
        >
          <el-option
            v-for="(item, index) in machines"
            :key="index"
            :label="item.name + ' - ' + item.code"
            :value="item.code"
          />
        </el-select>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="血袋号" prop="patient_sample">
            <el-input
              v-model="formData.patient_sample"
              clearable
              disabled
              placeholder="血袋号"
              style="width: 220px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="时间" prop="time">
            <el-date-picker
              v-model="formData.time"
              align="center"
              placeholder="选择日期时间"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="抗A" prop="AntiA">
            <el-select
              v-model="formData.AntiA"
              clearable
              filterable
              placeholder="抗A"
            >
              <el-option
                v-for="(item, index) in bloodResults"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="抗B" prop="AntiB">
            <el-select
              v-model="formData.AntiB"
              clearable
              filterable
              placeholder="抗B"
            >
              <el-option
                v-for="(item, index) in bloodResults"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="Ctrl" prop="Ctrl">
            <el-select
              v-model="formData.Ctrl"
              clearable
              filterable
              placeholder="Ctrl"
            >
              <el-option
                v-for="(item, index) in bloodResults"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="Ac" prop="Ac">
            <el-select
              v-model="formData.Ac"
              clearable
              filterable
              placeholder="Ac"
            >
              <el-option
                v-for="(item, index) in bloodResults"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="Bc" prop="Bc">
            <el-select
              v-model="formData.Bc"
              clearable
              filterable
              placeholder="Bc"
            >
              <el-option
                v-for="(item, index) in bloodResults"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="Rh(D)" prop="RhD">
            <el-select
              v-model="formData.RhD"
              clearable
              filterable
              placeholder="Rh(D)"
            >
              <el-option
                v-for="(item, index) in bloodRhds"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="ABO" prop="ABO">
            <el-select
              v-model="formData.ABO"
              clearable
              filterable
              placeholder="ABO"
            >
              <el-option
                v-for="(item, index) in bloodGroups"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="抗筛" prop="ABScr">
            <el-select
              v-model="formData.ABScr"
              clearable
              filterable
              placeholder="抗筛"
            >
              <el-option
                v-for="(item, index) in bloodSops"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="AntiD" prop="AntiD">
            <el-select
              v-model="formData.AntiD"
              clearable
              filterable
              placeholder="AntiD"
            >
              <el-option
                v-for="(item, index) in bloodResults"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="AbsI1" prop="AbsI1">
            <el-select
              v-model="formData.AbsI1"
              clearable
              filterable
              placeholder="AbsI1"
            >
              <el-option
                v-for="(item, index) in bloodResults"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="AbsI2" prop="AbsI2">
            <el-select
              v-model="formData.AbsI2"
              clearable
              filterable
              placeholder="AbsI2"
            >
              <el-option
                v-for="(item, index) in bloodResults"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="AbsI3" prop="AbsI3">
            <el-select
              v-model="formData.AbsI3"
              clearable
              filterable
              placeholder="AbsI3"
            >
              <el-option
                v-for="(item, index) in bloodResults"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="descri">
        <el-input
          v-model="formData.descri"
          clearable
          placeholder="备注"
          rows="3"
          type="textarea"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit('mainForm')">提交</el-button>
        <el-button @click="reset('mainForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
  import { updateOne as doEdit, addOne as doAdd } from '@/api/inspectAbo'
  import {
    machines,
    bloodGroups,
    bloodRhds,
    bloodResults,
    bloodSops,
  } from '@/utils/constant'
  import { removeEmptyField } from '@/utils'
  export default {
    data() {
      return {
        machineVisible: false,
        formData: {
          blood_id: '',
          ABO: '',
          AntiA: '',
          AntiB: '',
          Ctrl: '',
          Ac: '',
          Bc: '',
          RhD: '',
          ABScr: '',
          time: '',
          descri: '',
        },
        rules: {
          time: [{ required: true, trigger: 'blur', message: '时间必须' }],
          blood_id: [
            { required: true, trigger: 'blur', message: '血袋号必须' },
          ],
        },
        activeName: 'first',
        machines,
        bloodGroups,
        bloodRhds,
        bloodResults,
        bloodSops,
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.formData = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.formData = {
          blood_id: '',
          ABO: '',
          AntiA: '',
          AntiB: '',
          Ctrl: '',
          Ac: '',
          Bc: '',
          RhD: '',
          ABScr: '',
          time: '',
          descri: '',
        }
        this.dialogFormVisible = false
      },
      submit() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.title === '编辑') {
              let { msg } = await doEdit(
                this.formData.blood_id,
                removeEmptyField(this.formData)
              )
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            } else {
              let { msg } = await doAdd(removeEmptyField(this.formData))
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
